package com.fgs.common.widget.itemView;

import android.content.Context;
import android.content.res.ColorStateList;
import android.content.res.TypedArray;
import android.text.Editable;
import android.text.InputFilter;
import android.text.InputType;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.text.method.DigitsKeyListener;
import android.util.AttributeSet;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.fgs.common.R;
import com.fgs.common.interf.InputItemType;
import com.fgs.common.textWatcher.DecimalTextWatcher;
import com.fgs.common.textWatcher.InputTextWatcher;
import com.fgs.common.widget.AlphabetReplaceMethod;


/**
 * @author fgs
 * @date 2020/3/29
 * @description
 **/
public class InputItemView extends LinearLayout {
    private TextView mTipTextView;
    private TextView mTitleTextView;
    private EditText mContentEditText;
    private String mTitle;
    private float mTitleTextViewLength;
    private String mContent;
    private String mContentHint;
    private int mTitleTextColor;
    private int mContentBackground;

    private boolean isInputAble;
    //是否限制输入字符
    private boolean isNeedWatcher;
    //是否大写
    private boolean isCaps;
    private OnInputItemListener onInputItemListener;


    public InputItemView(Context context) {
        super(context);
        initView(context, null);
    }

    public InputItemView(Context context, AttributeSet attrs) {
        super(context, attrs);
        initView(context, attrs);
    }

    private void initView(Context context, AttributeSet attrs) {
        TypedArray typedArray = context.obtainStyledAttributes(attrs, R.styleable.InputItemView);
        boolean isShowTipText;
        boolean isShowTittleText;
        int inputType;
        int maxLength;
        String digits;
        int contentTextGravity;
        int titleTextGravity;
        ColorStateList contentTextColor;
        int contentHintColor;
        try {
            mTitle = typedArray.getString(R.styleable.InputItemView_ItemTitle);
            mTitleTextViewLength = typedArray.getDimension(R.styleable.InputItemView_ItemTitleLength, -2);
            mContent = typedArray.getString(R.styleable.InputItemView_ItemContent);
            mContentBackground = typedArray.getResourceId(R.styleable.InputItemView_ItemContentBackground, R.drawable.shape_edit_text);
            mContentHint = typedArray.getString(R.styleable.InputItemView_ItemContentHint);
            mTitleTextColor = typedArray.getColor(R.styleable.InputItemView_ItemTitleTextColor, context.getResources().getColor(R.color.colorTextBlack));
            contentTextColor = typedArray.getColorStateList(R.styleable.InputItemView_ItemContentTextColor);
            contentHintColor = typedArray.getColor(R.styleable.InputItemView_ItemContentHintTextColor, context.getResources().getColor(R.color.colorTextGray));

            isShowTipText = typedArray.getBoolean(R.styleable.InputItemView_isTipShow, false);
            isShowTittleText = typedArray.getBoolean(R.styleable.InputItemView_isTittleShow, true);
            isNeedWatcher = typedArray.getBoolean(R.styleable.InputItemView_isNeedWatcher, false);
            isInputAble = typedArray.getBoolean(R.styleable.InputItemView_isInputAble, true);
            isCaps = typedArray.getBoolean(R.styleable.InputItemView_isCaps, false);

            inputType = typedArray.getInt(R.styleable.InputItemView_ItemInputType, InputItemType.TEXT);
            maxLength = typedArray.getInt(R.styleable.InputItemView_ItemMaxLength, 500);
            digits = typedArray.getString(R.styleable.InputItemView_ItemDigits);
            contentTextGravity = typedArray.getInt(R.styleable.InputItemView_ItemContentGravity, 1);
            titleTextGravity = typedArray.getInt(R.styleable.InputItemView_ItemTitleGravity, 0);
        } finally {
            typedArray.recycle();
        }


        View view = LayoutInflater.from(context).inflate(R.layout.layout_input_item, null);
        LayoutParams layoutParams = new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT);
        view.setLayoutParams(layoutParams);
        addView(view);

        mTipTextView = view.findViewById(R.id.layout_input_item_tipTextView);
        mTitleTextView = view.findViewById(R.id.layout_input_item_titleTextView);
        mContentEditText = view.findViewById(R.id.layout_input_item_contentEditText);

        setTitle(mTitle);
        setTitleTextViewLength(mTitleTextViewLength);
        setContent(mContent);
        setContentBackground(mContentBackground);
        setContentHint(mContentHint);
        setTitleTextColor(mTitleTextColor);
        setContentTextColor(contentTextColor);
        setContentHintColor(contentHintColor);
        setShowTip(isShowTipText);
        setShowTittle(isShowTittleText);
        setItemEnable(isInputAble);
        setMaxLength(maxLength);
        setInputType(inputType);
        setDigits(digits);
        setContentTextGravity(contentTextGravity);
        setTitleTextGravity(titleTextGravity);

        mContentEditText.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {

            }

            @Override
            public void afterTextChanged(Editable s) {
                if (onInputItemListener != null) {
                    String string = s.toString();
                    if (!TextUtils.isEmpty(string)) {
                        onInputItemListener.onInput(s.toString());
                    } else {
                        onInputItemListener.onInput("");
                    }
                }
            }
        });
    }

    private void setContentBackground(int background) {
        if (background != 0) {
            mContentEditText.setBackgroundResource(background);
        }
    }

    private void setTitleTextViewLength(float titleLength) {
        ViewGroup.LayoutParams layoutParams;
        layoutParams = mTipTextView.getLayoutParams();
        if (titleLength == -1) {
            layoutParams.width = LayoutParams.MATCH_PARENT;
        } else if (titleLength == -2) {
            layoutParams.width = LayoutParams.WRAP_CONTENT;
        } else {
            layoutParams.width = (int) titleLength;
        }
        mTitleTextView.setLayoutParams(layoutParams);
    }

    private void setShowTittle(boolean isShowTittleText) {
        if (isShowTittleText) {
            mTitleTextView.setVisibility(View.VISIBLE);
        } else {
            mTitleTextView.setVisibility(View.GONE);
        }
    }

    private void setContentTextGravity(int gravity) {
        switch (gravity) {
            case 0:
                mContentEditText.setGravity(Gravity.LEFT);
                break;
            case 1:
                mContentEditText.setGravity(Gravity.RIGHT);
                break;
            case 2:
                mContentEditText.setGravity(Gravity.CENTER);
                break;
        }
    }

    private void setTitleTextGravity(int gravity) {
        switch (gravity) {
            case 0:
                mTitleTextView.setGravity(Gravity.LEFT);
                break;
            case 1:
                mTitleTextView.setGravity(Gravity.RIGHT);
                break;
            case 2:
                mTitleTextView.setGravity(Gravity.CENTER);
                break;
        }
    }

    private void setDigits(String digits) {
        if (TextUtils.isEmpty(digits)) return;
        mContentEditText.setKeyListener(DigitsKeyListener.getInstance(digits));
    }

    private void setMaxLength(int maxLength) {
        //手动设置maxLength为20
        InputFilter[] filters = {new InputFilter.LengthFilter(maxLength)};
        mContentEditText.setFilters(filters);

    }

    public void setInputType(int inputType) {
        switch (inputType) {
            case InputItemType.TEXT://字符
                if (isInputAble && isNeedWatcher) {
                    mContentEditText.addTextChangedListener(new InputTextWatcher(mContentEditText));
                }
                if (isCaps) {
                    mContentEditText.setTransformationMethod(new AlphabetReplaceMethod());
                } else {
                    mContentEditText.setTransformationMethod(null);
                }
                mContentEditText.setInputType(InputType.TYPE_CLASS_TEXT);
                break;
            case InputItemType.NUMBER://数字
                mContentEditText.setInputType(InputType.TYPE_CLASS_NUMBER);
                break;
            case InputItemType.PASSWORD://密码
                if (isInputAble && isNeedWatcher) {
                    mContentEditText.addTextChangedListener(new InputTextWatcher(mContentEditText));
                }
                mContentEditText.setInputType(InputType.TYPE_CLASS_TEXT | InputType.TYPE_TEXT_VARIATION_PASSWORD);
                break;
            case InputItemType.DECIMAL://小数
                mContentEditText.setInputType(InputType.TYPE_CLASS_NUMBER | InputType.TYPE_NUMBER_FLAG_DECIMAL);
                break;
            case InputItemType.MULTI_LINE://多行
                if (isInputAble && isNeedWatcher) {
                    mContentEditText.addTextChangedListener(new InputTextWatcher(mContentEditText));
                }
                mContentEditText.setInputType(InputType.TYPE_CLASS_TEXT | InputType.TYPE_TEXT_FLAG_MULTI_LINE | InputType.TYPE_TEXT_FLAG_IME_MULTI_LINE);
                break;
            case InputItemType.AMOUNT://金额
                mContentEditText.addTextChangedListener(new DecimalTextWatcher(mContentEditText));
                mContentEditText.setInputType(InputType.TYPE_CLASS_NUMBER | InputType.TYPE_NUMBER_FLAG_DECIMAL);
                setMaxLength(8);
                break;
            case InputItemType.WEIGHT://重量
                mContentEditText.addTextChangedListener(new DecimalTextWatcher(mContentEditText, 3));
                mContentEditText.setInputType(InputType.TYPE_CLASS_NUMBER | InputType.TYPE_NUMBER_FLAG_DECIMAL);
                setMaxLength(8);
                break;
            case InputItemType.PHONE://手机
                mContentEditText.setInputType(InputType.TYPE_CLASS_NUMBER);
                setMaxLength(11);
                break;
        }
    }


    public String getTitle() {
        return mTitle;
    }

    public void setTitle(String title) {
        this.mTitle = title;
        mTitleTextView.setText(title);
    }

    public String getContent() {
        String content = mContentEditText.getText().toString().trim();
        if (!TextUtils.isEmpty(content) && isCaps) {
            content = content.toUpperCase();
        }
        return content;
    }

    public void setContent(String content) {
        this.mContent = content;
        if (mContentEditText != null) {
            mContentEditText.setText(content);
        }
    }

    public String getContentHint() {
        return mContentHint;
    }

    public void setContentHint(String contentHint) {
        this.mContentHint = contentHint;
        if (mContentEditText != null && !TextUtils.isEmpty(contentHint)) {
            mContentEditText.setHint(contentHint);
        }
    }

    public int getTitleTextColor() {
        return mTitleTextColor;
    }

    public void setTitleTextColor(int titleTextColor) {
        this.mTitleTextColor = titleTextColor;
        if (mTitleTextView != null)
            mTitleTextView.setTextColor(titleTextColor);
    }

    public void setContentTextColor(ColorStateList contentTextColor) {
        if (mContentEditText != null && isInputAble && contentTextColor != null)
            mContentEditText.setTextColor(contentTextColor);
    }

    public void setContentTextColor(int contentColor) {
        if (mContentEditText != null && isInputAble)
            mContentEditText.setTextColor(contentColor);
    }
    public void setContentTextColorTwo(int contentColor) {
        if (mContentEditText != null )
            mContentEditText.setTextColor(contentColor);
    }

    public void setContentHintColor(int contentHintColor) {
        if (mContentEditText != null)
            mContentEditText.setHintTextColor(contentHintColor);
    }


    public void setShowTip(boolean isShowTipText) {
        if (mTipTextView != null) {
            if (isShowTipText) {
                mTipTextView.setVisibility(View.VISIBLE);
            } else {
                mTipTextView.setVisibility(View.INVISIBLE);
            }
        }
    }


    public void setItemEnable(boolean isEnable) {
        isInputAble = isEnable;
        if (mContentEditText != null)
            mContentEditText.setEnabled(isEnable);
    }

    public EditText getEditTextView() {
        return mContentEditText;
    }

    public interface OnInputItemListener {
        void onInput(String string);
    }

    public void setOnInputItemListener(OnInputItemListener onInputItemListener) {
        this.onInputItemListener = onInputItemListener;
    }
}
